<?php
class UserController extends CBaseController
{
	protected $basicUser;
	protected $validator;
	public function __construct(&$config=null) 
    { 
      parent::__construct($config);	  
    } 
	
	public function init($tpl)
	{
		parent::init($tpl);
		$this->templator->addViewsDir(dirname(__FILE__).'/../views');
		$this->prepare();
	}
	
	protected function prepare()
	{
		if(isset($_GET['id'])&&!empty($_GET['id']))
			$this->basicUser=CBaseOrm::load('basicuser',$_GET['id']);		
		else if(isset($_POST['id'])&&!empty($_POST['id']))		
			$this->basicUser=CBaseOrm::load('basicuser',$_POST['id']);
		else
			$this->basicUser=CBaseOrm::dispense('basicuser');
	}
	
	public function register()
	{
		$this->templator->assignByRef('basicUser',$this->basicUser);
		$this->templator->display('registration.html');
	}
	
	public function savePrepare()
	{
		if(isset($_POST['name'])&&!empty($_POST['name']))
		$this->basicUser->name=$_POST['name'];
		if(isset($_POST['email'])&&!empty($_POST['email']))
		$this->basicUser->email=$_POST['email'];
		if(isset($_POST['password'])&&!empty($_POST['password']))
		$this->basicUser->password=$_POST['password'];
	}
	
	public function saveORupdateValidate()
	{
		$this->validator = new FormValidator();
		$this->validator->addValidation("name","req","Please fill in Name");
		$this->validator->addValidation("email","req","Please fill in email");
		$this->validator->addValidation("email","email",null);
		$this->validator->addValidation("password","req","Please fill in password");
		return $this->validator->ValidateForm();
	}
	public function save()
	{
		$this->savePrepare();
		if($this->saveORupdateValidate())
		{
			CBaseOrm::store($this->basicUser);
			$this->templator->assign('info','User account created sucessfully');
			$this->templator->display('home.html');	
		}
	}
	
	
	
}	